package com.kanshu.ksgb.fastread.doudou.common.share;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.util.NetUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.ToastUtil;
import com.mob.MobSDK;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by wcy on 2017\11\2 0002
 */
public class ShareUtils {

    public static void share(Context context, ShareBean item, Dialog dialog) {
        if (!NetUtils.isNetworkAvailable(context)) {
            ToastUtil.showMessage(context, context.getString(R.string.standard_net_tip));
            return;
        }
        // 分享初始化
        MobSDK.init(context, context.getString(R.string.mob_app_appkey), context.getString(R.string.mob_app_secret));
        if (item == null) {
            return;
        }
        //
        if((TextUtils.equals(item.platform, WechatMoments.NAME) || TextUtils.equals(item.platform, Wechat.NAME)) && !isWXAppInstalledAndSupported(context)){
            ToastUtil.showMessage(context, context.getString(R.string.message_install_wechat));
        }
        Platform platform = ShareSDK.getPlatform(item.platform);
        Platform.ShareParams shareParams = new  Platform.ShareParams();
        shareParams.setText(item.text);
        shareParams.setTitle(item.title);
        shareParams.setUrl(item.url);
        shareParams.setTitleUrl(item.title_url);
        shareParams.setImageUrl(item.img_url);
        if (TextUtils.isEmpty(item.text) || TextUtils.isEmpty(item.img_url) || TextUtils.isEmpty(item.title)
                || TextUtils.isEmpty(item.url) || TextUtils.isEmpty(item.title_url)) {
            ToastUtil.showMessage("参数不全");
        }
        shareParams.setShareType(Platform.SHARE_WEBPAGE);
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Log.i("ssss","onComplete");
            }
            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Log.i("ssss","onError"+throwable);
                throwable.printStackTrace();
//                ShareEvent event = new ShareEvent();
//                // 由于第三方监听不准 这里只要拉起了第三方分享就算分享成功
//                //event.code = ShareEvent.FAILURE;
//                event.code = ShareEvent.SUCCESS;
//                EventBus.getDefault().post(event);
            }
            @Override
            public void onCancel(Platform platform, int i) {
                Log.i("ssss","oncancel");
//                ShareEvent event = new ShareEvent();
//                // 由于第三方监听不准 这里只要拉起了第三方分享就算分享成功
//                //event.code = ShareEvent.CANCEL;
//                event.code = ShareEvent.SUCCESS;
//                EventBus.getDefault().post(event);
            }
        });
        platform.share(shareParams);
    }

    private static boolean isWXAppInstalledAndSupported(Context context) {
        IWXAPI msgApi = WXAPIFactory.createWXAPI(context, null);
        msgApi.registerApp(context.getString(R.string.wx_app_id));
        return msgApi.isWXAppInstalled() && msgApi.isWXAppSupportAPI();
    }

    public static void share(Context context, ShareBean item) {
        share(context, item, null);
    }

}
